package com.web.controller;


import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 文件上传servelt
 */
@WebServlet("/upload")
public class FileUploadServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        //创建文件上传对象
//        ServletFileUpload upload = new ServletFileUpload();
//        //设置文件上传大小
//        upload.setFileSizeMax(10 * 1024 * 1024);
//        //判断请求中是否有上传文件
//        if(ServletFileUpload.isMultipartContent(req)){
//            //创建文件迭代器
//            try {
//                FileItemIterator itemIterator = upload.getItemIterator(req);
//                //遍历所有文件
//                while(itemIterator.hasNext()){
//                    //获得当前文件
//                    FileItemStream stream = itemIterator.next();
//                    //上传到OSS
//                    OssUtils.upload(stream.openStream(),stream.getName());
//                    //获得文件url
//                    String url = OssUtils.getURL(stream.getName());
//                    //发送url给前端
//                    resp.getWriter().write(JSON.toJSONString(new ResponseResult(200,"OK",url)));
//                }
//            } catch (FileUploadException e) {
//                throw new RuntimeException(e);
//            }
//        }
    }
}
